<?php

namespace app\admin\controller;

use think\Request;
use app\common\model\Banner as BannerModel ;

class Banner extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

	protected $btype = [
		'1' => '首页顶部轮播图',
		'2' => '首页中部轮播图',
		'3' => '首页团餐轮博图',
	];

    /**
     * @route('banner/bannerlist')
     * 轮播图列表
     */
    public function bannerlist()
    {
		$get['pos'] = input('get.pos');
		$BannerModel = new BannerModel();
        $banner = $BannerModel->get_list_page('*',$get,$this->limit);

        $page = $banner->render();

        $tvar = array(
            "title" => "轮播图列表",
            "time" => time(),
            "data" => $banner,
			'type' => $this->btype,
			'get' => $get,
        );
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('banner/bannerlist');
    }

    /**
     * @route('banner/boxbanner')
     * 编辑添加轮播图
     */
    public function boxbanner(Request $request)
    {
		$BannerModel = new BannerModel();
        if(\request()->isAjax()){
            $param = $request->param();
            $data = [
                'title' => $param['title'],
				'pos' => $param['pos'],
				'pic_url' => $param['images'],
//                'to_url' => $param['tourl'],
                'sort' => $param['sort'],
            ];

			$rs = $BannerModel->edit($data,$param['id']);
            if($rs === false){
                $this->error('操作失败');
            }
            $this->success('操作成功','banner/bannerlist');
        }
        $id = $_GET['id'];
        $banner = null;
        if($id != 0){
			$banner = $BannerModel->getOne(['id'=>$id]);
        }
        $tvar = array(
            "title" => "添加轮播图",
            "time"  => time(),
            "data" => $banner,
            "get" => $id,
        );
//        var_dump($tvar);die;
        $this->assign("data", $tvar);
        return $this->fetch('banner/boxbanner');
    }

    /**
     * @route('banner/delbanner')
     * 删除轮播图
     */
    public function delbanner()
    {
        $id = input('post.id');
		$BannerModel = new BannerModel();
        $rs = $BannerModel->Del($id);
//        if($rs === false){
//            $this->error("删除失败");
//        }
        $this->success('删除成功 ');
    }

    /**
     * 轮播图上传
     * @route('banner/upload')
     */
    public function bannerunpload()
    {
        $res = \Upload::FileUpload('file','banner',1024*1024*10);
        if(!$res['data']){
            $this->result([],-1,$res['msg']);
        }
        $this->result($res['data']['file_path'],1);
    }

}